<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2007/04/30 10:21:51 $)
// $RCSfile: functions_predefined.php,v $ : $Revision: 1.13 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Builds a category array for a provided staff
*/
function buildCategoryData(&$predefinedcategoryidlist, $predefineddata = false)
{
	global $dbCore, $_SWIFT;

	$categorydata = array();
	$catcount = 0;
	if (!$predefinedcategoryidlist)
	{
		$predefinedcategoryidlist = array();
	}

//	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."predefinedcategories` WHERE (categorytype = '". CATEGORY_PUBLIC ."') OR (categorytype = '". CATEGORY_PRIVATE ."' AND staffid = '". $dbCore->escape($_SWIFT["staff"]["staffid"]) ."') ORDER BY `predefinedcategoryid` ASC;");
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."predefinedcategories` ORDER BY `predefinedcategoryid` ASC;");
	while ($dbCore->nextRecord())
	{
		$catcount = count($categorydata[$dbCore->Record["parentcategoryid"]]);
		$categorydata[$dbCore->Record["parentcategoryid"]][$catcount] = $dbCore->Record;
		$predefineddata[$dbCore->Record["predefinedcategoryid"]] = $dbCore->Record;
	}

	return $categorydata;
}

/**
* Builds a list of predefined items from given categories
*/
function buildReplyData()
{
	global $dbCore, $_SWIFT;

	$replydata = array();

	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."predefinedreplies` AS predefinedreplies LEFT JOIN `". TABLE_PREFIX ."predefinedreplydata` AS predefinedreplydata ON (predefinedreplies.predefinedreplyid = predefinedreplydata.predefinedreplyid) ORDER BY predefinedreplies.predefinedreplyid ASC;");
	while ($dbCore->nextRecord())
	{
		$replydata[$dbCore->Record["predefinedcategoryid"]][] = $dbCore->Record;
	}

	return $replydata;
}

/**
* Builds the predefined xml
*/
function buildPredefinedXML(&$xml)
{
	$xml->addParentTag("predefined");
		$xml->addParentTag("category");
			$xml->addTag("predefinedcategoryid", "0");
			$xml->addTag("title", "Parent");
			$replydata = buildReplyData();
			$categorydata = buildCategoryData(($_temp=array()));
			doPRMenuXMLLoop(0, $categorydata, $replydata, $menudata, $xml, 1);

		$xml->endTag("category");
	$xml->endTag("predefined");
}

/**
* Process the articles and generate relevant JS
*/
function processMenuPRRepliesXML($predefinedcategoryid, $replydata, $childindent, &$xml)
{
	$replycount = count($replydata[$predefinedcategoryid]);
	if ($replycount != 0)
	{
		for ($ii=0; $ii<$replycount; $ii++)
		{
			$childdata = &$replydata[$predefinedcategoryid][$ii];
			$index = $ii+$index;

			$xml->addParentTag("reply");
				$xml->addTag("predefinedreplyid", $childdata["predefinedreplyid"]);
				$xml->addTag("predefinedcategoryid", $childdata["predefinedcategoryid"]);
				$xml->addTag("subject", $childdata["subject"]);
				$xml->addTag("contents", $childdata["contents"]);
			$xml->endParentTag("reply");

		}
	}
}

/**
* Does a loop on categories and data to generate the menu
*/
function doPRMenuXMLLoop($categoryid, $categorydata, $replydata, &$menudata, &$xml, $indent=0)
{
	global $_SWIFT;
	
	$childcount = count($categorydata[$categoryid]);
	$replycount = count($replydata[$categoryid]);
	if (!is_array($_option))
	{
		$_option = array();
	}
	

	// ======= PROCESS ARTICLES =======
	if ($replycount != 0)
	{
		processMenuPRRepliesXML($categoryid, $replydata, $childindent, $xml);
	}

	if ($childcount != 0)
	{
		// We itterate through all the childs
		$wrapindex = 0;
		for ($ii=0; $ii<$childcount; $ii++)
		{
			$childdata = &$categorydata[$categoryid][$ii];
			$index = $ii+$index;

			// Check the count of subchilds for this
			$subchilds = count($categorydata[$childdata["predefinedcategoryid"]]);

			$xml->addParentTag("category");
				$xml->addTag("predefinedcategoryid", $childdata["predefinedcategoryid"]);
				$xml->addTag("title", $childdata["title"]);

			// Does it have any sub childs?
			if ($subchilds > 0)
			{
				// Yup, do another loop for this
				$index++;
				doPRMenuXMLLoop($childdata["predefinedcategoryid"], $categorydata, $replydata, $menudata, $xml, ($indent+2));
			} else if ($subchilds == 0 && count($replydata[$childdata["predefinedcategoryid"]]) != 0) {
				// We have just replies under this category
				// Yup, do another loop for this
				$index++;
				$menudata .= processMenuPRRepliesXML($childdata["predefinedcategoryid"], $replydata, str_repeat("  ", ($indent+2)), $xml);
			}
			$xml->endParentTag("category");
		}
	}
}

/**
* Rebuilds the predefined javascript cache
*/
function rebuildPredefinedJSCache()
{
	global $_SWIFT;

	// ======= FIRST BUILD ALL THE ARTICLE DATA =======
	$replydata = buildReplyData();
	$jsdata = "prmenuloaded = true;";
	// ======= NOW BUILD THE MENU =======
	$jsdata .= SWIFT_CRLF.SWIFT_CRLF;
	$categorydata = buildCategoryData(($_temp=array()));
	$jsdata .= 'var MENU_JSCACHE_PREDEFINED = ['.SWIFT_CRLF;
	$jsdata .= '	{"popup":1, "popupoff":[0,0], "pos":[0,0], "size":[22,250], "itemoff":[21,0], "leveloff":[10,59],dynamic: true, https_fix_blank_doc:themepath+"blank.html", "delay":600, "style":STYLEPOPUP, "imgsize":[20,22], wrapoff:["level","+previousItem-1px"]},'.SWIFT_CRLF;
	$menudata = "";
	$jsdata .= doPRMenuLoop(0, $categorydata, $replydata, $menudata, 1);
	$jsdata .= '];'.SWIFT_CRLF;
	insertToJSCache("predefined", $jsdata);
}

/**
* Does a loop on categories and data to generate the menu
*/
function doPRMenuLoop($categoryid, $categorydata, $replydata, &$menudata, $indent=0)
{
	global $_SWIFT;
	
	$childcount = count($categorydata[$categoryid]);
	$replycount = count($replydata[$categoryid]);
	if (!is_array($_option))
	{
		$_option = array();
	}
	
	$childindent = str_repeat("  ", $indent);
	
	// ======= PROCESS ARTICLES =======
	if ($replycount != 0)
	{
		$menudata2 = processMenuPRReplies($categoryid, $replydata, $childindent);
	}

	if ($childcount != 0)
	{
		// We itterate through all the childs
		$wrapindex = 0;
		for ($ii=0; $ii<$childcount; $ii++)
		{
			$childdata = &$categorydata[$categoryid][$ii];
			$index = $ii+$index;

			// Check the count of subchilds for this
			$subchilds = count($categorydata[$childdata["predefinedcategoryid"]]);

			if ($wrapindex >= 15)
			{
				$dowrap = true;
				$wrapindex = 0;
			} else {
				$dowrap = false;
			}
			if ($ii == 0 || $dowrap == true)
			{
				$mstyle = "VSTYLEBORDER";
			} else {
				$mstyle = "VSTYLE";
			}

			$menudata .= $childindent.'{code:"'. addslashes($childdata["title"]) .'"'. iif($dowrap, ', wrapPoint:true') .', url:"javascript:void(0);", "format":{'. iif($categoryid==0&&$ii==0, '"itemoff":[0,0], ', '"itemoff":[21,0], ') .'"arrow":themepath+"icon_rightarrowgray.gif", "oarrow":themepath+"icon_rightarrowgray.gif", "arrsize":[10,10], "image":themepath+"icon_menufolder.gif", "oimage":themepath+"icon_menufolder.gif", "style":'. $mstyle .'}';
			$wrapindex++;

			// Does it have any sub childs?
			if ($subchilds > 0)
			{
				$childindentfw = str_repeat("  ", ($indent+1));
				$childindentfw2 = str_repeat("  ", ($indent+2));
				$menudata .= ",".SWIFT_CRLF;
				$menudata .= $childindentfw.'sub:['.SWIFT_CRLF;
				$menudata .= $childindentfw2 . '{"leveloff":[20,90], wrapoff:[\'level\',\'+previousItem-1px\']},'.SWIFT_CRLF;

				// Yup, do another loop for this
				$index++;
				doPRMenuLoop($childdata["predefinedcategoryid"], $categorydata, $replydata, $menudata, ($indent+2));
				$menudata .= $childindentfw.']'.SWIFT_CRLF;
				if (($ii+1) == $childcount && $replycount == 0)
				{
					$menudata .= $childindent.'}'.SWIFT_CRLF;
				} else {
					$menudata .= $childindent.'},'.SWIFT_CRLF;
				}
			} else if ($subchilds == 0 && count($replydata[$childdata["predefinedcategoryid"]]) != 0) {
				// We have just replies under this category
				$childindentfw = str_repeat("  ", ($indent+1));
				$childindentfw2 = str_repeat("  ", ($indent+2));
				$menudata .= ",".SWIFT_CRLF;
				$menudata .= $childindentfw.'sub:['.SWIFT_CRLF;
				$menudata .= $childindentfw2 . '{"leveloff":[20,90], wrapoff:[\'level\',\'+previousItem-1px\']},'.SWIFT_CRLF;

				// Yup, do another loop for this
				$index++;
				$menudata .= processMenuPRReplies($childdata["predefinedcategoryid"], $replydata, str_repeat("  ", ($indent+2)));
				$menudata .= $childindentfw.']'.SWIFT_CRLF;
				if (($ii+1) == $childcount && $replycount == 0)
				{
					$menudata .= $childindent.'}'.SWIFT_CRLF;
				} else {
					$menudata .= $childindent.'},'.SWIFT_CRLF;
				}
			} else {
				if (($ii+1) == $childcount && $replycount == 0)
				{
					$menudata .= "}".SWIFT_CRLF;
				} else {
					$menudata .= "},".SWIFT_CRLF;
				}
			}
		}
	}

	$menudata .= $menudata2;
	
	return $menudata;
}

/**
* Process the articles and generate relevant JS
*/
function processMenuPRReplies($predefinedcategoryid, $replydata, $childindent)
{
	$replycount = count($replydata[$predefinedcategoryid]);
	if ($replycount != 0)
	{
		$wrapindex = 0;
		for ($ii=0; $ii<$replycount; $ii++)
		{
			$childdata = &$replydata[$predefinedcategoryid][$ii];
			$index = $ii+$index;

			$aimage = "icon_menukbarticle.gif";
			if ($wrapindex >= 15)
			{
				$dowrap = true;
				$wrapindex = 0;
			} else {
				$dowrap = false;
			}
			if ($ii == 0 || $dowrap == true)
			{
				$mstyle = "VSTYLEBORDER";
			} else {
				$mstyle = "VSTYLE";
			}
			$menudata2 .= $childindent.'{code:"'. addslashes(iif(strlen($childdata["subject"])>30, substr($childdata["subject"],0,30)."...", $childdata["subject"])) .'"'. iif($dowrap, ', wrapPoint:true') .', url:"javascript:void(autoInsertPR('. intval($childdata["predefinedreplyid"]) .'));", "format":{'. iif($predefinedcategoryid!=0, '"itemoff":[21,0], ', '') .'"image":themepath+"'. $aimage .'", "oimage":themepath+"'. $aimage .'", "style":'. $mstyle .'}';
			$wrapindex++;

			if (($ii+1) == $replycount)
			{
				$menudata2 .= "}".SWIFT_CRLF;
			} else {
				$menudata2 .= "},".SWIFT_CRLF;
			}
		}

		return $menudata2;
	}
}

/**
* Builds a treeview like category select data
*/
function buildCategorySelectData($selectedcatid=0)
{
	global $_SWIFT;
	
	$_option = doCategoryOptionLoop(buildCategoryData(($_temp=array())), 0, $selectedcatid, 1, ($index=1), ($_temp=array()));

	$_option[0]["title"] = $_SWIFT["language"]["parentcategory"];
	$_option[0]["value"] = "0";
	$_option[0]["selected"] = iif($selectedcatid==0, true, false);

	return $_option;
}

/**
* Does a loop on categories to generate data
*/
function doCategoryOptionLoop($data, $parentid, $selectedcatid=0, $indent=0, &$index, &$_option)
{
	$childcount = count($data[$parentid]);
	if (!is_array($_option))
	{
		$_option = array();
	}
	
	if ($childcount != 0)
	{
		$childindent = str_repeat("   ", $indent);
		$childprefix = $childindent."|- ";

		// Iterate through all childs and build the option list
		for ($ii=0; $ii<$childcount; $ii++)
		{
			$childdata = &$data[$parentid][$ii];
			$_option[$index]["title"] = $childprefix.$childdata["title"];
			$_option[$index]["value"] = $childdata["predefinedcategoryid"];
			$_option[$index]["selected"] = iif($selectedcatid == $childdata["predefinedcategoryid"], true, false);
			$index++;

			// Does it have any sub childs?
			if (count($data[$childdata["predefinedcategoryid"]]) > 0)
			{
				// Yup, do another loop for this
				doCategoryOptionLoop($data, $childdata["predefinedcategoryid"], $selectedcatid, ($indent+1), $index, $_option);
			}
		}
	}
	
	return $_option;
}

/**
* Builds a tree view with category data
*/
function buildCategoryTreeData(&$nodeclass, $selectedcatid=0)
{
	return doCategoryTreeLoop(buildCategoryData(($_temp=array())), 0, $nodeclass, $selectedcatid);
}

/**
* Does a loop on categories to generate tree view data
*/
function doCategoryTreeLoop($data, $parentid, &$nodeclass, $selectedcatid=0)
{
	global $_SWIFT;

	$childcount = count($data[$parentid]);
	if (!is_array($_option))
	{
		$_option = array();
	}
	
	if ($childcount != 0)
	{
		for ($ii=0; $ii<$childcount; $ii++)
		{
			$childdata = &$data[$parentid][$ii];
			$index = $ii+$index;
			$subchilds = count($data[$childdata["predefinedcategoryid"]]);

			$_newnode = &$nodeclass->addItem(new HTML_TreeNode(array('text' => "&nbsp;". $childdata["title"] ."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ <a href='index.php?_m=tickets&_a=insertpredcat&parentcategoryid=". $childdata["predefinedcategoryid"] ."' id='blue' title='". $_SWIFT["language"]["addsubcat"] ."'>". $_SWIFT["language"]["addsubcat"] ."</a> | <a href='index.php?_m=tickets&_a=insertpredreply&parentcategoryid=". $childdata["predefinedcategoryid"] ."' id='blue' title='". $_SWIFT["language"]["addreply"] ."'>". $_SWIFT["language"]["addreply"] ."</a> | <a href='index.php?_m=tickets&_a=editpredcat&predefinedcategoryid=". $childdata["predefinedcategoryid"] ."' id='blue' title='". $_SWIFT["language"]["editcategory"] ."'>". $_SWIFT["language"]["editcategory"] ."</a> | <a onClick='javascript:doConfirm(\"". $_SWIFT["language"]["catdelactionconfirm"] ."\", \"index.php?_m=tickets&_a=managepredcat&do=delete&predefinedcategoryid=". $childdata["predefinedcategoryid"] ."\");' href='#' id='blue' title='". $_SWIFT["language"]["delete"] ."'>". $_SWIFT["language"]["delete"] ."</a> | <a href='index.php?_m=tickets&_a=managepredreplies&parentcategoryid=". $childdata["predefinedcategoryid"] ."' id='blue' title='". $_SWIFT["language"]["viewprereplies"] ."'>". $_SWIFT["language"]["viewprereplies"] ."</a>]", 'link' => "", 'icon' => "icon_folderyellow3.gif", 'expandedIcon' => iif(!$subchilds, "icon_folderyellow3.gif", "icon_folderyellow3.gif"), 'expanded' => true)));

			// Does it have any sub childs?
			if ($subchilds > 0)
			{
				// Yup, do another loop for this
				$index++;
				doCategoryTreeLoop($data, $childdata["predefinedcategoryid"], $_newnode, $selectedcatid);
			}
		}
	}
	
	return true;
}

/**
* Deletes a category and all sub categories under it
*/
function deleteCategory($predefinedcategoryid)
{
	global $dbCore, $_SWIFT;

	$predefinedreplylist = $predefinedreplydatalist = array();
	
	$predefinedcategorylist = getSubCategoryList($predefinedcategoryid, buildCategoryData(($_temp=array())), ($_temp = array()));

	// Build the reply list now
	$dbCore->query("SELECT `predefinedreplyid` FROM `". TABLE_PREFIX ."predefinedreplies` WHERE `predefinedcategoryid` IN (". buildIN($predefinedcategorylist) .");");
	while ($dbCore->nextRecord())
	{
		$predefinedreplylist[] = $dbCore->Record["predefinedreplyid"];
	}

	// ok now start nuking them
	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."predefinedreplydata` WHERE `predefinedreplyid` IN (". buildIN($predefinedreplylist) .");");
	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."predefinedreplies` WHERE `predefinedreplyid` IN (". buildIN($predefinedreplylist) .");");
	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."predefinedcategories` WHERE `predefinedcategoryid` IN (". buildIN($predefinedcategorylist) .");");

	rebuildPredefinedJSCache();

	return true;
}

/**
* Returns the current category id + all sub categories under it
*/
function getSubCategoryList($predefinedcategoryid, $data, &$categorylist)
{
	global $dbCore, $_SWIFT;

	if (!$categorylist)
	{
		$categorylist = array();
		if ($predefinedcategoryid != 0)
		{
			$categorylist[] = $predefinedcategoryid;
		}
	}

	
	$childcount = count($data[$predefinedcategoryid]);
	
	if ($childcount != 0)
	{
		for ($ii=0; $ii<$childcount; $ii++)
		{
			$childdata = &$data[$predefinedcategoryid][$ii];
			$index = $ii+$index;

			$categorylist[] = $childdata["predefinedcategoryid"];

			// Does it have any sub childs?
			if (count($data[$childdata["predefinedcategoryid"]]) > 0)
			{
				// Yup, do another loop for this
				$index++;
				getSubCategoryList($childdata["predefinedcategoryid"], $data, $categorylist);
			}
		}
	}
	
	return $categorylist;
}

/**
* Delete a Predefined Reply
*/
function deletePredefinedReplies($predefinedreplyidlist)
{
	global $dbCore, $_SWIFT;

	if (!_is_array($predefinedreplyidlist))
	{
		return false;
	}

	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."predefinedreplydata` WHERE `predefinedreplyid` IN (". buildIN($predefinedreplyidlist) .");");
	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."predefinedreplies` WHERE `predefinedreplyid` IN (". buildIN($predefinedreplyidlist) .");");

	rebuildPredefinedJSCache();

	return true;
}

/**
* Add a new predefined category
*/
function insertPredefinedCategory($parentcategoryid, $title, $type)
{
	global $dbCore, $_SWIFT;

	$dbCore->query("INSERT INTO `". TABLE_PREFIX ."predefinedcategories` (`parentcategoryid`, `categorytype`, `staffid`, `title`) VALUES ('". intval($parentcategoryid) ."', '". intval($type) ."', '". iif($type == CATEGORY_PRIVATE, intval($_SWIFT["staff"]["staffid"]), "0") ."', '". $dbCore->escape($title) ."');");

	rebuildPredefinedJSCache();

	return true;
}

/**
* Add a new predefined reply
*/
function insertPredefinedReply($predefinedcategoryid, $subject, $contents)
{
	global $dbCore, $_SWIFT;

	$dbCore->query("INSERT INTO `". TABLE_PREFIX ."predefinedreplies` (`predefinedcategoryid`, `staffid`, `subject`, `dateline`) VALUES ('". intval($predefinedcategoryid) ."', '". intval($_SWIFT["staff"]["staffid"]) ."', '". $dbCore->escape($subject) ."', '". DATENOW ."');");
	$predefinedreplyid = $dbCore->lastInsertId();

	$dbCore->query("INSERT INTO `". TABLE_PREFIX ."predefinedreplydata` (`predefinedreplyid`, `contents`) VALUES ('". $dbCore->escape($predefinedreplyid) ."', '". $dbCore->escape($contents) ."');");

	rebuildPredefinedJSCache();

	return true;
}

/**
* Update an existing category
*/
function updatePredefinedCategory($predefinedcategoryid, $parentcategoryid, $title, $type)
{
	global $dbCore, $_SWIFT;

	$dbCore->query("UPDATE `". TABLE_PREFIX ."predefinedcategories` SET `parentcategoryid` = '". intval($parentcategoryid) ."', `categorytype` = '". intval($type) ."', `staffid` = '". iif($type == CATEGORY_PRIVATE, intval($_SWIFT["staff"]["staffid"]), "0") ."', `title` = '". $dbCore->escape($title) ."' WHERE `predefinedcategoryid` = '". $dbCore->escape($predefinedcategoryid) ."';");

	rebuildPredefinedJSCache();

	return true;
}

/**
* Update Predefined Reply
*/
function updatePredefinedReply($predefinedreplyid, $predefinedcategoryid, $subject, $contents)
{
	global $dbCore, $_SWIFT;

	$dbCore->query("UPDATE `". TABLE_PREFIX ."predefinedreplies` SET `predefinedcategoryid` = '". $dbCore->escape($predefinedcategoryid) ."', `staffid` = '". intval($_SWIFT["staff"]["staffid"]) ."', `subject` = '". $dbCore->escape($subject) ."', `dateline` = '". DATENOW ."' WHERE `predefinedreplyid` = '". $dbCore->escape($predefinedreplyid) ."';");

	$dbCore->query("UPDATE `". TABLE_PREFIX ."predefinedreplydata` SET `contents` = '". $dbCore->escape($contents) ."' WHERE `predefinedreplyid` = '". $dbCore->escape($predefinedreplyid) ."';");

	rebuildPredefinedJSCache();

	return true;
}

/**
* Render the category edit/insert form
*/
function renderCategoryForm($type)
{
	global $_SWIFT;

	printFormStart();
	printMainTableHeader(iif($type==INSERT,$_SWIFT["language"]["insertcategory"], $_SWIFT["language"]["editcategory"]));

	printTextRow("title", $_SWIFT["language"]["cattitle"], $_SWIFT["language"]["desc_cattitle"], "text", $_POST["title"]);

	$_options = buildCategorySelectData(iif(empty($_REQUEST["parentcategoryid"]), 0, $_REQUEST["parentcategoryid"]));
	printSelectRow("parentcategoryid", $_SWIFT["language"]["parentcategory"], $_SWIFT["language"]["desc_parentcategory"], $_options);

//	printPublicPrivateRow("type", $_SWIFT["language"]["cattype"], $_SWIFT["language"]["desc_cattype"], iif(empty($_POST["type"]), true, $_POST["type"]));

	printSubmitRow(iif($type==INSERT, $_SWIFT["language"]["insertcategory"], $_SWIFT["language"]["updatecategory"]));

	printMainTableFooter();

	addHiddenField("_m", "tickets");
	addHiddenField("_a", iif($type==INSERT,"insertpredcat", "editpredcat"));
	addHiddenField("step", "1");
	if ($type == EDIT)
	{
		addHiddenField("predefinedcategoryid", $_REQUEST["predefinedcategoryid"]);
	}
	printFormEnd();
}

/**
* Render the reply edit/insert form
*/
function renderReplyForm($type)
{
	global $_SWIFT;

	printFormStart();
	printMainTableHeader(iif($type==INSERT,$_SWIFT["language"]["insertreply"], $_SWIFT["language"]["editreply"]));

	printTextRow("subject", $_SWIFT["language"]["replysubject"], $_SWIFT["language"]["desc_replysubject"], "text", $_POST["subject"]);

	$_options = buildCategorySelectData(iif(empty($_REQUEST["parentcategoryid"]), 0, $_REQUEST["parentcategoryid"]));
	printSelectRow("parentcategoryid", $_SWIFT["language"]["parentcategoryres"], $_SWIFT["language"]["desc_parentcategoryres"], $_options);

	printDescRow($_SWIFT["language"]["replycontents"]);

	if (!empty($_GET["ticketid"]) && !empty($_GET["ticketpostid"]))
	{
		// We are adding the data from an existing ticket we just submitted..
		require_once ("./modules/tickets/functions_ticketmain.php");
		$_ticketobj =& new TicketMain($_GET["ticketid"], true);
		if ($_ticketobj->ticket)
		{
			$_ticketpost = $_ticketobj->getTicketPost($_GET["ticketpostid"]);
			$_POST["contents"] = $_ticketpost["contents"];
			addHiddenField("ticketid", $_GET["ticketid"]);
			addHiddenField("ticketpostid", $_GET["ticketpostid"]);
			addHiddenField("departmentid", $_GET["departmentid"]);
			addHiddenField("ticketstatusid", $_GET["ticketstatusid"]);
			addHiddenField("opt_addkb", $_GET["opt_addkb"]);
		}
	}

	$data = array();
	$data[0]["value"] = '<textarea style="WIDTH:100%;" name="contents" cols="100" rows="15">'. htmlspecialchars($_POST["contents"]) .'</textarea>'.SWIFT_CRLF;
	$data[0]["align"] = "center";
	$data[0]["colspan"] = "2";

	printDataRow($data);
	printSubmitRow(iif($type==INSERT,$_SWIFT["language"]["insertreply"], $_SWIFT["language"]["updatereply"]));

	printMainTableFooter();

	addHiddenField("_m", "tickets");
	addHiddenField("_a", iif($type==INSERT,"insertpredreply", "editpredreply"));
	addHiddenField("step", "1");
	if ($type == EDIT)
	{
		addHiddenField("predefinedreplyid", $_REQUEST["predefinedreplyid"]);
	}
	printFormEnd();
}
?>